/**
 * FileName: LoginController
 * Author:   P
 * Date:     2021/4/22 10:41
 * Description:
 */
package com.hospital.nurse.controller;

import com.hospital.nurse.pojo.Nurses;
import com.hospital.nurse.service.NursesService;
import com.hospital.nurse.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private NursesService nursesService;


    @RequestMapping("/toLogin")
    public Map<String,Object> toLogin(@RequestBody Nurses nurses){
        Map<String, Object> map = new HashMap<>();
        boolean b = nursesService.loginValid(nurses);
        if (b){
            Nurses user = nursesService.getNurse(nurses.getUsername());
            Map<String, String>  payload= new HashMap<>();
            payload.put("nid",""+user.getNId());
            payload.put("did",""+user.getDId());
            payload.put("username",""+user.getUsername());
            payload.put("nname",""+user.getNName());
            String token = JWTUtils.getToke(payload);
            map.put("token",token);
        }
        map.put("msg",b);
        return map;
    }

}
